package com.lly.songhigh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.dao.Collection;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 李新伟 on 15/11/7.
 *
 */
public class CollectionListAdapter extends RecyclerView.Adapter<CollectionListAdapter.ViewHolder> {

    private Context context;
    private List<Collection> collections;
    private OnRelativeClickLinstener onRelativeClickLinstener;
    private List<String> albumPics;
    private OnRelativeLongClickLinstener onRelativeLongClickLinstener;

    public void setOnRelativeLongClickLinstener(OnRelativeLongClickLinstener onRelativeLongClickLinstener) {
        this.onRelativeLongClickLinstener = onRelativeLongClickLinstener;
    }

    public void setOnRelativeClickLinstener(OnRelativeClickLinstener onRelativeClickLinstener) {
        this.onRelativeClickLinstener = onRelativeClickLinstener;
    }

    public void removeData(Collection collection) {
        collections.remove(collection);
        notifyDataSetChanged();
    }

    public void refreshData(List<Collection> collections, List<String> albumPics) {
        this.collections = collections;
        this.albumPics = albumPics;
        notifyDataSetChanged();
    }

    public interface OnRelativeLongClickLinstener {
        void onRelativeLongClick(int position, Collection collection);
    }
    public interface OnRelativeClickLinstener{
        void onRelativeClick(int position, Collection collection);
    }

    public CollectionListAdapter(Context context, List<Collection> collections, List<String> albumPics) {
        this.context = context;
        this.collections = collections;
        this.albumPics = albumPics;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.collection_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Collection collection = collections.get(position);
        holder.collectionSongName.setText(collection.getSongName());
        holder.collectionSingerName.setText(collection.getSingerName());
        if (albumPics.get(position) != null)
            Picasso.with(context).load(albumPics.get(position)).into(holder.collectionImg);
        holder.collectionItemRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRelativeClickLinstener != null) {
                    onRelativeClickLinstener.onRelativeClick(position, collections.get(position));
                }
            }
        });
        holder.collectionItemRelative.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onRelativeLongClickLinstener != null) {
                    onRelativeLongClickLinstener.onRelativeLongClick(position, collections.get(position));
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return collections != null && collections.size() > 0 ? collections.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private CircleImageView collectionImg;
        private TextView collectionSongName;
        private TextView collectionSingerName;
        private RelativeLayout collectionItemRelative;

        public ViewHolder(View itemView) {
            super(itemView);
            collectionImg = (CircleImageView) itemView.findViewById(R.id.collection_item_img);
            collectionSongName = (TextView) itemView.findViewById(R.id.collection_songName);
            collectionSingerName = (TextView) itemView.findViewById(R.id.collection_singerName);
            collectionItemRelative = (RelativeLayout) itemView.findViewById(R.id.collection_item_relative);
        }
    }
}
